package com.rzjm.api.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rzjm.api.entity.User;
import com.rzjm.api.entity.WxTemplate;
import com.rzjm.api.pay.wxpay.WXPayCommonUtil;
import org.springframework.beans.factory.annotation.Value;


public class WxMessage {

    @Value("${web.appid}")
    private String appid;
    @Value("${web.secret}")
    private String secret;

    /**
     * 获取ACCESSTOKEN
     * @param APPID
     * @param APPSECRET
     * @return
     */
    public static String getACCESSTOKEN(String APPID,String APPSECRET){
        String access_token = "";
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "GET","");
        System.out.println("获取accessToken " + result);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(jsonObject!=null && jsonObject.size() > 0){
            access_token = jsonObject.containsKey("access_token") ? jsonObject.getStr("access_token"): "";
        }
        return access_token;
    }

    public static JSONObject getUserMpInfo(String ACCESS_TOKEN,String OPENID){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ACCESS_TOKEN+"&openid="+OPENID+"&lang=zh_CN";
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "GET","");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }


    public static JSONObject getUserMp(String ACCESS_TOKEN,String NEXT_OPENID){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+ACCESS_TOKEN+"&next_openid="+NEXT_OPENID;
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "GET","");
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }

    public static JSONObject sendMsg(String ACCESS_TOKEN,String param){
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ACCESS_TOKEN;
        String result = WXPayCommonUtil.httpsRequest(requestUrl, "POST", param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        return jsonObject;
    }

    /**
     * 根据模板消息的 short_id  获取 模板的id
     * @param template_id_short
     * @return
     */
    public String getTemplateId(String template_id_short,String ACCESS_TOKEN){
        String template_id = "";

        String requestUrl = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token="+ACCESS_TOKEN;
        String param = "{\"template_id_short\":\""+template_id_short+"\"}";

        String result = WXPayCommonUtil.httpsRequest(requestUrl, "POST",param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if(jsonObject!=null && jsonObject.size() > 0){
            int errcode = jsonObject.getInt("errcode");
            String errmsg = jsonObject.getStr("errmsg");
            template_id = jsonObject.containsKey("template_id") ? jsonObject.getStr("template_id"): "";
        }
        return template_id;
    }



    /**
     *  给微信管理员用户推送报修的消息
     * @param user
     * @param temp_id_mini
     * @param formId
     * @return
     */
    private WxTemplate getOrderSuccessWxTemplate(User user, String temp_id_mini, String formId){
//        WxTemplate t = new WxTemplate();
//        t.setPage("pages/orderlist/orderlist?_from=personal&state=1&index=2&orderStatus=WAIT_BUYER_PAY");
//        t.setForm_id(formId);
//        t.setTouser(user.getOpenId());
//        t.setTemplate_id(temp_id_mini);
//        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
////                物品名称 {{keyword1.DATA}}
////                订单编号 {{keyword2.DATA}}
////                付款金额 {{keyword3.DATA}}
////                付款时间 {{keyword4.DATA}}
//        TemplateData keyword1 = new TemplateData();
//        keyword1.setValue(product.getProductName());
//        m.put("keyword1", keyword1);
//
//        TemplateData keyword2 = new TemplateData();
//        keyword2.setValue(order.getOrderNo());
//        m.put("keyword2", keyword2);
//
//        TemplateData keyword3 = new TemplateData();
//        keyword3.setValue(String.valueOf(order.getPayMoney())+"元");
//        m.put("keyword3", keyword3);
//
//        TemplateData keyword4 = new TemplateData();
//        keyword4.setValue(Tools.date2Str(order.getPayTime(),"yyyy-MM-dd HH:mm:ss"));
//        m.put("keyword4", keyword4);
//
//        t.setData(m);
//        return t;
        return null;
    }
    public static void main(String args[]){

        String accessToken = getACCESSTOKEN("wx4023ebd185405f69","d2b3a8f37a51dd902316ecf0a0ac0711");
        String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;

        System.out.println("accessToken:" + accessToken);
        JSONObject jsonObject = getUserMp(accessToken,"");
        if(jsonObject!=null){
            System.out.println(jsonObject.toString());
        }
    }
}
